﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace LevenBlog.Core.Utils
{
    public static class Text
    {
        private static string[] icons ={"asp","bmp","css","doc","gif","htm","html","jpg",
                                          "jpeg","png","js","mbk","mdb","mp3","pdf","swf",
                                          "wma","wmv","xml","xls","zip","rar","7z","gz","bz2"};

        public static string UrlEncode(this string s)
        {
            return HttpContext.Current.Server.HtmlEncode(s);
        }

        public static string UrlDecode(this string s)
        {
            return HttpContext.Current.Server.HtmlDecode(s);
        }

        public static string HtmlEncode(this string s)
        {
            s = HttpContext.Current.Server.HtmlEncode(s);
            //s=Regex.Replace(s, @"\r*\n", "<br />");
            s = s.Replace("\r\n", "<br />");
            s = s.Replace("\r", "<br />");
            s = s.Replace("\n", "<br />");
            return s;
        }

        public static string HtmlDecode(this string s)
        {
            return HttpContext.Current.Server.HtmlDecode(s);
        }

        public static string SqlEncode(this string s)
        {
            StringBuilder sb = new StringBuilder(s);
            sb.Replace("'", "''");
            return sb.ToString();
        }

        public static int ToInt32(this string s, int defaultvalue)
        {
            int.TryParse(s, out defaultvalue);
            return defaultvalue;
        }

        public static int ToInt32(this string s)
        {
            return s.ToInt32(0);
        }

        public static string FormEncode(this string s)
        {
            if (s == null)
                return string.Empty;
            else
            {
                return s.Trim();
            }
        }

        public static string ToMD5(this string s)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
        }

        public static string AddPrefix(this string s)
        {
            return WebConfig.PrefixKey + s;
        }

        public static string RemovePrefix(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;
            else if (s.Length < WebConfig.PrefixKey.Length)
                return s;
            else
                return s.Substring(WebConfig.PrefixKey.Length);
        }

        public static bool ToBoolean(this string s)
        {
            bool result = false;
            bool.TryParse(s, out result);
            return result;
        }

        public static bool ToBoolean(this int i)
        {
            if (i > 0)
                return true;
            else
                return false;
        }

        public static string ToCheckString(this bool b)
        {
            if (b)
                return " checked=\"checked\"";
            else
                return string.Empty;
        }

        public static string ToSafeHtml(this string s)
        {
            //StringBuilder sb = new StringBuilder(s);
            //sb.Replace("<script", "&lt;script");
            //sb.Replace("</script>", "&lt;/script&gt;");
            //sb.Replace("<object", "&lt;object");
            //sb.Replace("</object>", "&lt;/object&gt;");
            //sb.Replace("<style", "&lt;style");
            //sb.Replace("</style", "&lt;/style&gt;");
            //sb.Replace("<link", "&lt;link");
            //sb.Replace("</link>", "&lt;/link&gt;");
            //sb.Replace("<iframe", "&lt;iframe");
            //sb.Replace("</iframe>", "&lt;/iframe&gt;");
            //sb.Replace("<body", "&lt;body");
            //sb.Replace("</body>", "&lt;/body&gt;");
            //sb.Replace("<html", "&lt;html");
            //sb.Replace("</html>", "&lt/html&gt;");
            //return sb.ToString();
            s = Regex.Replace(s, @"<\s*(\/?)\s*(script|i?frame|style|html|body|title|link|meta|object|\?|\%)([^>]*?)>", string.Empty, RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)", "$1>", RegexOptions.IgnoreCase);
            return s;
        }

        public static string RemoveHtml(this string s)
        {
            return Regex.Replace(s, @"<\/?[^>]+>", string.Empty);
        }

        public static string ToFullUrl(this string s)
        {
            s = s.Replace('\\', '/');
            if (s[s.Length - 1] == '/')
                s = s.Substring(0, s.Length - 1);
            if (!s.StartsWith("http://") && !s.StartsWith("https://"))
            {
                s = "http://" + s;
            }
            return s;
        }

        public static string AppendPath(this string s, string path, char split)
        {
            if (s == null)
                return split.ToString();
            s = s.Replace('\\', split).Replace('/', split);
            if (s[s.Length - 1] != split)
                s += split.ToString();
            s += path;
            return s;
        }

        public static string AppendPath(this string s, string path)
        {
            return s.AppendPath(path, '\\');
        }

        public static string ToFileName(this string s)
        {
            StringBuilder sb = new StringBuilder(s);
            sb.Replace("/", string.Empty);
            sb.Replace("\\", string.Empty);
            sb.Replace(":", string.Empty);
            sb.Replace("*", string.Empty);
            sb.Replace("?", string.Empty);
            sb.Replace(",", string.Empty);
            sb.Replace("<", string.Empty);
            sb.Replace(">", string.Empty);
            sb.Replace("|", string.Empty);
            return sb.ToString();
        }

        public static string ToFileIcon(this string s)
        {
            if (icons.Count(t => t == s) < 1)
            {
                return "unknow";
            }
            else
            {
                return s;
            }
        }

        public static string ToCheckString(this string s)
        {
            s = s.Trim().ToLower();
            if (s == "true" || s == "1")
                return true.ToCheckString();
            else
                return false.ToCheckString();
        }

        public static bool IsBooleanString(this string s)
        {
            s = s.Trim().ToLower();
            if (s == "true" || s == "false")
                return true;
            else
                return false;
        }

        public static bool IsMail(this string s) {
            string pattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            return Regex.IsMatch(s, pattern);
        }
    }
}
